package com.ruoyi.blog.controller;

import com.ruoyi.blog.domain.BlogType;
import com.ruoyi.blog.service.IBlogTypeService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.excel.ExcelUtils;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 文章类型Controller
 *
 * @author 刘哲贤
 * @date 2022-08-15
 */
@RestController
@RequestMapping("/admin/type")
public class BlogTypeController extends BaseController {
    @Resource
    private IBlogTypeService blogTypeService;

    /**
     * 查询文章类型列表
     */
    @PreAuthorize("@ss.hasPermi('admin:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlogType blogType) {
        startPage();
        List<BlogType> list = blogTypeService.selectBlogTypeList(blogType);
        return getDataTable(list);
    }

    /**
     * 导出文章类型列表
     */
    @PreAuthorize("@ss.hasPermi('admin:type:export')")
    @Log(title = "文章类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlogType blogType) {
        List<BlogType> list = blogTypeService.selectBlogTypeList(blogType);
        ExcelUtils<BlogType> util = new ExcelUtils<BlogType>(BlogType.class);
        util.exportData(response, list, "文章类型数据");
    }

    /**
     * 获取文章类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(blogTypeService.selectBlogTypeById(id));
    }

    /**
     * 新增文章类型
     */
    @PreAuthorize("@ss.hasPermi('admin:type:add')")
    @Log(title = "文章类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlogType blogType) {
        return toAjax(blogTypeService.insertBlogType(blogType));
    }

    /**
     * 修改文章类型
     */
    @PreAuthorize("@ss.hasPermi('admin:type:edit')")
    @Log(title = "文章类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlogType blogType) {
        return toAjax(blogTypeService.updateBlogType(blogType));
    }

    /**
     * 删除文章类型
     */
    @PreAuthorize("@ss.hasPermi('admin:type:remove')")
    @Log(title = "文章类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(blogTypeService.deleteBlogTypeByIds(ids));
    }

    /**
     * 查询文章类型列表不分页
     */
    @GetMapping("/typeList")
    public TableDataInfo typeList(BlogType blogType) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        blogType.setDeleted(0);
        List<BlogType> list = blogTypeService.selectBlogTypeList(blogType);
        tableDataInfo.setRows(list);
        return tableDataInfo;
    }
}
